<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start();
class Admin extends CI_Controller{
  
  private function loadModel(){
    $this->load->model('admin_model','admin',TRUE);
    //$this->load->model('category_model','category',TRUE);
  }
  public function index(){
    
  }
  public function crudCategory(){
    if(isset($_SESSION['id'])){
      $this->load->view('header');
      $this->load->view('crud_category');
      $this->load->view('footer');
    }
    else {
      $this->auth();
    }
  }
  public function crudItem(){
    if(isset($_SESSION['id'])){
      $this->load->view('header');
      $this->load->view('crud_item');
      $this->load->view('footer');
    }
    else {
      $this->auth();
    }
  }

  public function login(){
    $this->loadModel();
    //$param = array('login' => 'asdd', 'password' => 'asdd');
    $data =json_decode($this->admin->log($_POST),true);
    if($data['code']){
      //вообще-то в индекс надо
      $this->crudCategory();
    }
  }
  public function registration(){
    $this->loadModel();
    //$param = array('login' => 'asdd', 'password' => 'asdd');
    $this->admin->reg($_POST);
  }
  public function auth(){
    if(isset($_SESSION['id'])){
      echo '{"code": 1, "msg": "Вы уже входили"}';
    }
    else{
      $this->load->view('header');
      $this->load->view('admin_login');
      $this->load->view('footer');
    }
  }
}